package com.qy.ibp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.mybatis.generator.config.ColumnOverride;
import org.mybatis.generator.config.IgnoredColumn;

import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.GeneratorConfig;
import com.zzg.mybatis.generator.model.UITableColumnVO;
import com.zzg.mybatis.generator.util.DbUtil;
import com.zzg.mybatis.generator.util.MyFileUtils;
import com.zzg.mybatis.generator.util.StringUtil;
import com.zzg.mybatis.generator.view.UIProgressCallback2;

public class MainGenByCode {

	static String projectFolder = "D:\\mybtis_code";
	static String modelPackage = "com.banggood.adg.ms.web.ad.po";
	static String daoPackage = "com.banggood.adg.ms.web.ad.mapper";

//	static String dbType = "MySQL";
//	static String host = "192.168.1.52";
//	static String userName = "root";
//	static String password = "123456";
//	static String port = "8066";
//	static String schema = "ad_data";

	static String dbType = "MySQL";
	static String host = "localhost";
	static String userName = "root";
	static String password = "123456";
	static String port = "3306";
	static String schema = "ms_upms";
	
	
	public static GeneratorConfig generatorConfig = new GeneratorConfig();

	public static DatabaseConfig databaseConfig = new DatabaseConfig();

	
	//不需要生成的字段
	static List<String> ignoredModelColumnList = Arrays.asList("id","deleteEnum","isDeleted","lockVersion","createDate","createUserId","createUserName","modifyDate","modifyUserId","modifyUserName","deleteDate","deleteUserId","deleteUserName");

	public static void init() {
		setConfig();
		setDatabaseConfig();
	}
	public static void setConfig() {
		generatorConfig.setProjectFolder(projectFolder);
		generatorConfig.setModelPackage(modelPackage);
		generatorConfig.setGenerateKeys("");
		generatorConfig.setModelPackageTargetFolder("src/main/java");
		generatorConfig.setDaoPackage(daoPackage);
		generatorConfig.setDaoTargetFolder("src/main/java");
		generatorConfig.setMapperName("");
		generatorConfig.setMappingXMLPackage("mapper");
		generatorConfig.setMappingXMLTargetFolder("src/main/resources");

		generatorConfig.setOffsetLimit(false);
		generatorConfig.setComment(true);
		generatorConfig.setNeedToStringHashcodeEquals(false);
		generatorConfig.setAnnotation(false);
		generatorConfig.setUseActualColumnNames(false);// TODO 待调试
		generatorConfig.setEncoding("UTF-8");
		generatorConfig.setUseExampe(false);
	}

	public static void setDatabaseConfig() {
		databaseConfig.setDbType(dbType);
		databaseConfig.setHost(host);
		databaseConfig.setUsername(userName);
		databaseConfig.setPassword(password);
		databaseConfig.setPort(port);
		databaseConfig.setEncoding("utf8");
		databaseConfig.setSchema(schema);
	}

	public static void main(String[] args) throws Exception {
		init();

		List<String> tableList = DbUtil.getTableNames(databaseConfig);

		MyFileUtils.deleteAndCheckDirs(generatorConfig);

		for (String t : tableList) {
			
			try {
				
				generatorConfig.setTableName(t);
				generatorConfig.setDomainObjectName(StringUtil.underline2Camel(t, false));
				
				List<UITableColumnVO> columnList = DbUtil.getTableColumns(databaseConfig, t);
				
				// 设置列
				List<ColumnOverride> columnOverrides = new ArrayList();
				List<IgnoredColumn> ignoredColumns = new ArrayList<>();
				columnList.forEach(column -> {
					String columnNameCamel = StringUtil.underline2Camel(column.getColumnName(), true);
					ColumnOverride columnOverride = new ColumnOverride(column.getColumnName());
					columnOverride.setTypeHandler(column.getTypeHandle());
					columnOverride.setJavaProperty(columnNameCamel);
					columnOverride.setJavaType(column.getJavaType());
					columnOverrides.add(columnOverride);
					
				});
				
				MybatisGeneratorBridge batisGeneratorBridge = new MybatisGeneratorBridge();
				batisGeneratorBridge.setDatabaseConfig(databaseConfig);
				batisGeneratorBridge.setIgnoredColumns(ignoredColumns);
				batisGeneratorBridge.setProgressCallback(new UIProgressCallback2());
				batisGeneratorBridge.setGeneratorConfig(generatorConfig);
				batisGeneratorBridge.setColumnOverrides(columnOverrides);
				batisGeneratorBridge.setIgnoredModelColumnList(ignoredModelColumnList);
				batisGeneratorBridge.generate();
			} catch (Exception e) {
				// TODO: handle exception
			}
			

		}
	}

   
}
